# (1)	题目描述：
# 通过OpenCV读取一张图片，完成下面的操作：1、滤波 2、二值化 3、霍夫直线检测 4、仿射变换
# (2)	题目要求：.
# ①　导入相关头文件
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from python_ai.common.xcommon import *

spr = 2
spc = 4
spn = 0
plt.figure(figsize=[12, 6])


def my_show_pic(img, title):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    plt.axis('off')
    plt.imshow(img, cmap='gray')
    plt.title(title)


# ②　读入一张图片并转为灰度图
img_path = '../../../../../large_data/pic/white_block_precise.png'
# img_path = '../../../../../large_data/pic/white_block.png'
img = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
my_show_pic(img, 'ori')

res = cv.Sobel(img, cv.CV_8U, 1, 0, ksize=5)
my_show_pic(res, 'Sobel x cv.CV_8U')
print_numpy_ndarray_info(res, 'Sobel x cv.CV_8U')

res = cv.Sobel(img, cv.CV_64F, 1, 0, ksize=5)
my_show_pic(res, 'Sobel x cv.CV_64F')
print_numpy_ndarray_info(res, 'Sobel x cv.CV_64F')

res_abs = np.absolute(res)
print_numpy_ndarray_info(res_abs, 'res_abs')
my_show_pic(res_abs, 'res_abs')

res_8u = np.uint8(res_abs)
print_numpy_ndarray_info(res_8u, 'res_8u')
my_show_pic(res_8u, 'res_8u')

plt.show()
